package GUI;

import javax.swing.JPanel;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import Business.Musik;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;

import dao.MusikDao;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public class VerwalteGUI extends JPanel implements ActionListener {
	private JLabel labelLiderverwalten;
	private JLabel labelTitel;
	private JLabel labelGenre;
	private JLabel labelNewLabel;
	private JLabel labelNewLabel_1;
	private JLabel labelNewLabel_2;
	private Musik musik;
	private JTextField textFieldTitel;
	private JTextField textFieldGenre;
	private JTextField textFieldInter;
	private JTextField textFieldAlbum;
	private JTextField textFieldJahr;
	private JButton buttonndern;
	private MusikDao musikdao;
	private MP3File mp3;
	/**
	 * Create the panel.
	 */
	/*
	public VerwalteGUI(Musik musik) {
		this.musik = musik;
		initGUI();
		fuellen();
	}
	 */
	public VerwalteGUI(Musik musik) {

		initGUI();
		this.musik = musik;
		
		try {
			musikdao = new MusikDao();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			musik = new Musik();
		}
		try {
			set();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

//	public void fuellen(Musik musik) {
//
//
//		//if(!musik.getTitel().equals("")){
//		textFieldTitel.setText(musik.getTitel());
//		/*}else{
//			textFieldTitel.setText(null);
//		}
//		 */	
//		textFieldGenre.setText(Integer.toString(musik.getGenreID()));
//		textFieldInter.setText(Integer.toString (musik.getInterpretID()));
//		textFieldAlbum.setText(Integer.toString(musik.getAlbumID()));
//		textFielddauer.setText(Integer.toString(musik.getTime()));
//		textFieldJahr.setText(Integer.toString(musik.getJahr()));
//	}
	private void initGUI() {
		setLayout(null);
		{
			labelLiderverwalten = new JLabel("Liederverwalten");
			labelLiderverwalten.setFont(new Font("Tahoma", Font.PLAIN, 16));
			labelLiderverwalten.setHorizontalAlignment(SwingConstants.CENTER);
			labelLiderverwalten.setBounds(62, 11, 155, 23);
			add(labelLiderverwalten);
		}
		{
			labelTitel = new JLabel("Titel ");
			labelTitel.setBounds(25, 60, 46, 14);
			add(labelTitel);
		}
		{
			labelGenre = new JLabel("Genre");
			labelGenre.setBounds(25, 124, 46, 14);
			add(labelGenre);
		}
		{
			labelNewLabel = new JLabel("Interpret");
			labelNewLabel.setBounds(25, 93, 75, 14);
			add(labelNewLabel);
		}
		{
			labelNewLabel_1 = new JLabel("Album");
			labelNewLabel_1.setBounds(25, 155, 46, 14);
			add(labelNewLabel_1);
		}
		{
			labelNewLabel_2 = new JLabel("Jahr");
			labelNewLabel_2.setBounds(25, 186, 46, 14);
			add(labelNewLabel_2);
		}
		{
			textFieldTitel = new JTextField();
			textFieldTitel.setBounds(81, 57, 243, 20);
			add(textFieldTitel);
			textFieldTitel.setColumns(10);
		}
		{
			textFieldGenre = new JTextField();
			textFieldGenre.setBounds(81, 121, 243, 20);
			add(textFieldGenre);
			textFieldGenre.setColumns(10);
		}
		{
			textFieldInter = new JTextField();
			textFieldInter.setBounds(81, 90, 243, 20);
			add(textFieldInter);
			textFieldInter.setColumns(10);
		}
		{
			textFieldAlbum = new JTextField();
			textFieldAlbum.setBounds(81, 152, 243, 20);
			add(textFieldAlbum);
			textFieldAlbum.setColumns(10);
		}
		{
			textFieldJahr = new JTextField();
			textFieldJahr.setBounds(81, 183, 243, 20);
			add(textFieldJahr);
			textFieldJahr.setColumns(10);
		}
		{
			buttonndern = new JButton("\u00C4ndern");
			buttonndern.addActionListener(this);
			buttonndern.setBounds(81, 214, 243, 26);
			add(buttonndern);
		}
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonndern) {
			do_buttonndern_actionPerformed(e);
		}
	}
	public void set() throws IOException, TagException{
		
		textFieldTitel.setText(musik.getTitel());
		textFieldInter.setText(musik.getInterpret());
		textFieldGenre.setText(musik.getGenre());
		textFieldAlbum.setText(musik.getAlbum());
		textFieldJahr.setText(Integer.toString(musik.getJahr()));
		
		File a = new File(musik.getUrl());
		mp3 = new MP3File(a);
		
		
	}
	
	

	public void get(){
		
		musik.setTitel(textFieldTitel.getText());
		musik.setAlbumID(Integer.parseInt(textFieldAlbum.getText()));
		musik.setGenreID(Integer.parseInt(textFieldGenre.getText()));
		musik.setInterpretID(Integer.parseInt(textFieldInter.getText()));
		musik.setJahr(Integer.parseInt(textFieldJahr.getText()));
		
		
		
	}
	protected void do_buttonndern_actionPerformed(ActionEvent e) {

		get();
		
		try {
			//musikdao.update(musik);
			mp3.getID3v1Tag().setTitle(musik.getTitel());
			mp3.getID3v1Tag().setArtist(musik.getInterpret());
			//mp3.getID3v1Tag().setGenre(Integer.bitCount(musik.getGenre()));
			mp3.getID3v1Tag().setAlbum(musik.getAlbum());
			mp3.getID3v1Tag().setYear(Integer.toString(musik.getJahr()));
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
}
